package com.boboPlanet.comm.utils;

import java.util.List;

/**
 * @Description: TODO 一些常用方法工具类
 * @Author: mis_wu
 * @Date: 2023/3/9
 **/
public class CommUtils {

    /**
     * 手动分页处理
     * @param list List分页集合
     * @param currentPage Integer 当前页
     * @param pageSize Integer 每页数
     */
    public static void pageHandle(List<Object> list, Integer currentPage, Integer pageSize){
        if (list == null || list.size() == 0){
            return;
        }
        Integer totals = list.size();
        int totalPage = totals % pageSize;
        boolean flag = false;
        if (totalPage == 0){
            totalPage = totals / pageSize;
            //整页为true
            flag = true;
        }else {
            //向下取整
            totalPage = (int) (Math.floor(totals / pageSize) + 1);
        }
        int startIndex = (currentPage - 1) * pageSize;
        int endIndex = currentPage * pageSize;
        //最后页处理
        if (currentPage < totalPage){
            list = list.subList(startIndex, endIndex);
        }
        if (currentPage == totalPage && flag){
            list = list.subList(startIndex, endIndex);
        }
        if (currentPage == totalPage && !flag){
            list = list.subList(startIndex, totals);
        }
        if (currentPage > totalPage){
            throw new RuntimeException("分页参数错误");
        }
    }
}
